Ext.define('Seq.exporter.data.Table', {
	override : 'Ext.exporter.data.Table',

	getColumnLevels : function(columns, depth, result, topDown) {
		var col, i, j, len, name, level, cols;

		if (!columns) {
			return;
		}

		len = columns.length;
		for (i = 0; i < len; i++) {
			col = columns.items[i];
			level = col.getLevel();
			cols = col.getColumns();

			name = 's' + level;
			result[name] = result[name] || [];
			result[name].push(col);

			if (!cols) {
				for (j = level + 1; j <= depth; j++) {
					name = 's' + j;
					result[name] = result[name] || [];
					result[name].push(topDown ? col : null);
				}
			} else {
				for (j = 0; j < cols.length; j++) { // TODO，修改列序号
					if (col._index) {
						cols.items[j].setIndex(col.getIndex() + j);
					}
				}
				this.getColumnLevels(cols, depth, result, topDown);
			}
		}
	},

	syncColumns : function() {
		var cols = this.getColumns(), depth = this.getColDepth(cols, -1), result = {}, i, j, length, len, keys, arr, prevCol, index;

		if (!cols) {
			return;
		}
		length = cols.length;
		for (i = 0; i < length; i++) {
			cols.items[i].sync(depth);
		}
		this.getColumnLevels(cols, depth, result);
		keys = Ext.Object.getKeys(result);
		length = keys.length;

		for (i = 0; i < length; i++) {
			arr = result[keys[i]];
			len = arr.length;
			for (j = 0; j < len; j++) {
				if (j === 0) {
					index = arr[j].getIndex() || 1; // 
				} else if (arr[j - 1]) {
					prevCol = arr[j - 1].getConfig();
					index += (prevCol.mergeAcross ? prevCol.mergeAcross + 1 : 1);
				} else {
					index++;
				}
				if (arr[j]) {
					arr[j].setIndex(index);
				}
			}
		}
	}
});
